package com.example.testapp.ui.notifications

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.testapp.databinding.FragmentNotificationsBinding
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.formatter.ValueFormatter
import com.github.mikephil.charting.utils.ColorTemplate

class NotificationsFragment : Fragment() {

    private var _binding: FragmentNotificationsBinding? = null
    private val binding get() = _binding!!
    
    private lateinit var pieChart: PieChart
    private lateinit var barChart: BarChart
    
    // 月度消费数据
    private val monthlyExpenses = floatArrayOf(3500f, 4100f, 3600f, 4300f, 4200f, 4600f)
    private val totalExpense = 4303.00f  // 总消费

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val notificationsViewModel =
            ViewModelProvider(this).get(NotificationsViewModel::class.java)

        _binding = FragmentNotificationsBinding.inflate(inflater, container, false)
        val root: View = binding.root

        // 获取图表引用
        pieChart = binding.pieChart
        barChart = binding.barChart

        // 初始化饼图
        setupPieChart()
        loadPieChartData()

        // 初始化柱状图
        setupBarChart()
        loadBarChartData()

        // 原始代码保留
        val textView = binding.textNotifications
        notificationsViewModel.text.observe(viewLifecycleOwner) {
            textView.text = it
        }
        
        return root
    }

    private fun setupPieChart() {
        pieChart.apply {
            isDrawHoleEnabled = true
            setUsePercentValues(true)
            setEntryLabelTextSize(12f)
            setEntryLabelColor(Color.BLACK)
            centerText = "消费分布"
            setCenterTextSize(20f)
            description.isEnabled = false
        }
        
        pieChart.legend.apply {
            verticalAlignment = Legend.LegendVerticalAlignment.TOP
            horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
            orientation = Legend.LegendOrientation.VERTICAL
            setDrawInside(false)
            isEnabled = true
        }
    }
    
    private fun loadPieChartData() {
        val entries = ArrayList<PieEntry>().apply {
            // 添加示例数据，与图片中保持一致
            add(PieEntry(1280f, "餐饮"))
            add(PieEntry(2565f, "购物"))
            add(PieEntry(458f, "交通"))
        }
        
        val colors = ArrayList<Int>().apply {
            for (color in ColorTemplate.MATERIAL_COLORS) {
                add(color)
            }
        }
        
        val dataSet = PieDataSet(entries, "消费类别").apply {
            setColors(colors)
        }
        
        val data = PieData(dataSet).apply {
            setDrawValues(true)
            setValueTextSize(12f)
            setValueTextColor(Color.BLACK)
            setValueFormatter(object : ValueFormatter() {
                override fun getFormattedValue(value: Float): String {
                    return "¥${String.format("%.2f", value)}"
                }
            })
        }
        
        pieChart.apply {
            this.data = data
            invalidate()
            animateY(1400)
        }
    }
    
    private fun setupBarChart() {
        barChart.apply {
            setDrawBarShadow(false)
            setDrawValueAboveBar(true)
            description.isEnabled = false
            setPinchZoom(false)
            setDrawGridBackground(false)
        }
        
        // 设置X轴
        barChart.xAxis.apply {
            position = XAxis.XAxisPosition.BOTTOM
            granularity = 1f
            setDrawGridLines(false)
            setDrawAxisLine(true)
            
            // 设置月份作为X轴标签
            val months = arrayOf("1月", "2月", "3月", "4月", "5月", "6月")
            valueFormatter = IndexAxisValueFormatter(months)
        }
        
        // 设置左侧Y轴
        barChart.axisLeft.apply {
            setDrawGridLines(true)
            axisMinimum = 0f // 设置最小值为0
            axisMaximum = 5000f // 根据数据设置最大值
            setDrawZeroLine(false)
        }
        
        // 隐藏右侧Y轴
        barChart.axisRight.isEnabled = false
        
        // 设置图例
        barChart.legend.isEnabled = false // 不需要图例
    }
    
    private fun loadBarChartData() {
        val entries = ArrayList<BarEntry>().apply {
            // 添加柱状图数据
            for (i in monthlyExpenses.indices) {
                add(BarEntry(i.toFloat(), monthlyExpenses[i]))
            }
        }
        
        val dataSet = BarDataSet(entries, "月度消费").apply {
            color = Color.parseColor("#2196F3") // 蓝色柱状图
            setDrawValues(true)
        }
        
        val data = BarData(dataSet).apply {
            setValueTextSize(10f)
            setValueTextColor(Color.BLACK)
            barWidth = 0.6f // 设置柱子宽度
            setValueFormatter(object : ValueFormatter() {
                override fun getFormattedValue(value: Float): String {
                    return if (value > 0) {
                        String.format("%.0f", value)
                    } else {
                        ""
                    }
                }
            })
        }
        
        barChart.apply {
            this.data = data
            invalidate()
            animateY(1400)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
} 